package com.controller;

import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.entity.Brand;
import com.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.annotation.IgnoreAuth;
import com.entity.view.BrandView;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;


/**
 * 汽车种类
 * 后端接口
 * @author
 * @email
 * @date 2023-12-12 13:37:41
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;







    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, Brand brand,
                  HttpServletRequest request){
        EntityWrapper<Brand> ew = new EntityWrapper<Brand>();

        PageUtils page = brandService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, brand), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,Brand brand,
                  HttpServletRequest request){
        EntityWrapper<Brand> ew = new EntityWrapper<Brand>();

        PageUtils page = brandService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, brand), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( Brand brand){
        EntityWrapper<Brand> ew = new EntityWrapper<Brand>();
        ew.allEq(MPUtil.allEQMapPre( brand, "brand"));
        return R.ok().put("data", brandService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(Brand brand){
        EntityWrapper< Brand> ew = new EntityWrapper< Brand>();
        ew.allEq(MPUtil.allEQMapPre( brand, "brand"));
        BrandView shangpinzhongleiView =  brandService.selectView(ew);
        return R.ok("查询汽车种类成功").put("data", shangpinzhongleiView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        Brand brand = brandService.selectById(id);
        return R.ok().put("data", brand);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id){
        Brand brand = brandService.selectById(id);
        return R.ok().put("data", brand);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Brand brand, HttpServletRequest request){
        //ValidatorUtils.validateEntity(brand);
        brandService.insert(brand);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody Brand brand, HttpServletRequest request){
        //ValidatorUtils.validateEntity(brand);
        brandService.insert(brand);
        return R.ok();
    }




    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody Brand brand, HttpServletRequest request){
        //ValidatorUtils.validateEntity(brand);
        brandService.updateById(brand);//全部更新
        return R.ok();
    }





    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        brandService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }












}
